<?php

namespace App\Commons\Domain\Module\UserCenter\Service;

use App\Commons\Domain\Core\DomainService;
use App\Commons\Domain\Module\UserCenter\Context\AccountContext;
use App\Commons\Domain\Module\UserCenter\Entity\UserEntity;
use App\Commons\Domain\Module\UserCenter\Model\Admin;
use App\Commons\Domain\Module\UserCenter\Model\Member;
use App\Commons\Domain\Module\UserCenter\Traits\AdminLoginTrait;
use App\Commons\Domain\Module\UserCenter\Traits\MemberLoginTrait;
use App\Commons\Domain\Traits\DomainMgr\DomainManageable;
use \App\Commons\Domain\Module\UserCenter\Contract\AccountService as AcService;
use Hash;

class AccountService extends DomainService implements AcService
{
    use MemberLoginTrait;
    use AdminLoginTrait;
    use DomainManageable;
    public const CONTEXT = AccountContext::class;
    const MANAGERS = ['member' => Member::class];

    /**
     * 用户注册
     * @param string $username
     * @param string $password
     * @param string $mobile
     * @param int $country_id
     * @param string $email
     * @return mixed
     */
    public function createUser(string $username, string $password, string $mobile, int $country_id, string $email = '')
    {
        return app(UserEntity::class)->createUser($username, $password, $mobile, $country_id, $email);
    }

    /**
     * 创建管理员
     * @param string $username
     * @param string $password
     * @return mixed
     */
    public function createAdmin(string $username, string $password)
    {
        return db_trans(function () use ($username, $password) {
            throw_on(app(Admin::class)->whereUsername($username)->exists(), 0xf00112);

            $user = app(Admin::class)->create(compact('username'));

            $password = Hash::make($password);
            $user->account()->create(compact('username', 'password'));

            return $user;
        });

    }

    /**
     * 修改用户信息
     * @param Member $user 当前登陆用户
     * @param string $nickname
     * @param string $avatar
     * @param int $gender
     * @return bool
     * @throws \Exception
     */
    public function updateUserInfo(Member $user, string $nickname, string $avatar, int $gender = 0)
    {
        $res = app(UserEntity::class)->updateUserInfo($user, $nickname, $avatar, $gender);
        $this->forgetLoginInfo($user);
        return $res;
    }

    /**
     * 忘记密码，重设密码
     * @param string $mobile
     * @param string $new_password
     * @param string $email
     * @return mixed
     */
    public function forgotPassword(string $mobile, string $new_password, string $email = '')
    {
        return app(UserEntity::class)->forgotPassword($mobile, $new_password, $email);
    }

    /**
     * 修改密码
     * @param Member $user 当前登陆用户
     * @param string $new_password
     * @return mixed
     */
    public function updatePassword($user, string $new_password)
    {
        return app(UserEntity::class)->updatePassword($user, $new_password);
    }

    /**
     * 修改通知状态
     * @param Member $user
     * @param string $field
     * @param int $status
     * @return mixed
     */
    public function switchNotice(Member $user, string $field, int $status)
    {
        return app(UserEntity::class)->switchNotice($user, $field, $status);
    }


    /**
     * @inheritDoc
     * @throws \Exception
     */
    public function changePwd(Member $member, string $password, string $password_repeat)
    {
        throw_on($password !== $password_repeat, 0xf00132);

        $res = app(UserEntity::class)->changePwd($member, $password);
        $this->forgetLoginInfo($member);
        return $res;
    }
}
